import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 主路由配置
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/views/index/index.vue')
  },
  // {
  //   path: '/news',
  //   name: 'News',
  //   component: () => import('@/views/news/index.vue')
  // },
  // // 带ID的新闻路径
  // {
  //   path: '/news/:id',
  //   name: 'News',
  //   component: () => import('@/views/news/index.vue')
  // },
  // {
  //   path: '/product',
  //   name: 'Product',
  //   component: () => import('@/views/product/index.vue')
  // },
  // // 带ID的产品路径
  {
    path: '/product/:id',
    name: 'Product',
    component: () => import('@/views/product/index.vue')
  },
  {
    path: '/product-detail',
    name: 'ProductDetail',
    component: () => import('@/views/product/detail.vue')
  },
  {
    path: '/case',
    name: 'Case',
    component: () => import('@/views/case/index.vue')
  },
  {
    path: '/case-detail',
    name: 'CaseDetail',
    component: () => import('@/views/case/detail.vue')
  },
  // 带ID的案例路径
  {
    path: '/case/:id',
    name: 'Case',
    component: () => import('@/views/case/index.vue')
  },
  {
    path: '/news',
    name: 'News',
    component: () => import('@/views/news/index.vue')
  },
  {
    path: '/news-detail',
    name: 'NewsDetail',
    component: () => import('@/views/news/detail.vue')
  },
  // 带ID的案例路径
  {
    path: '/news/:id',
    name: 'News',
    component: () => import('@/views/news/index.vue')
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/views/about/index.vue')
  },
  // 带ID的关于我们路径
  {
    path: '/about/:id',
    name: 'About',
    component: () => import('@/views/about/index.vue')
  },
  {
    path: '/about-detail',
    name: 'AboutDetail',
    component: () => import('@/views/about/detail.vue')
  },
  // 捕获所有未匹配路由，重定向到首页
  {
    path: '*',
    redirect: '/'
  }
]

// 解决Vue Router在3.0版本以上重复导航时会报错的问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      return Promise.reject(err)
    }
    return Promise.resolve()
  })
}

// 创建路由实例
const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  // 滚动行为
  scrollBehavior() {
    return { x: 0, y: 0 }
  }
})

export default router 